<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of users_model
 *
 * @author chandu
 */
class users_model extends model {

    public $email;
    public $password;
    public $schoolname;
    public $mobile;
    public $status;
    public $key;
    public $data;
    private $change_pass;
    private $current_pass;
    private $current;

    public function __construct() {
        parent::__construct();
    }

    public function get_login($email, $password, $table) {

        //$this->error = new errors();
        $this->email = htmlentities($email);
        $this->password = htmlentities($password);
        //  $this->db = new dbconfig();
        $this->query = "SELECT * FROM $table WHERE email = '$this->email' AND password = '$this->password'";
        $this->result = $this->execute($this->query) or die("There is some problem in fetching the data for login");
        $this->result_data = $this->num_rows($this->result);
        if ($this->result_data == 1) {
            $this->result_data = $this->fetch_array($this->result);
            if ($this->result_data['status'] == "1") {

                $_SESSION['admin'] = $this->result_data['adminstatus'];
                $_SESSION['email'] = $this->email;
                $_SESSION['userid'] = $this->result_data['userid'];
                return "ok";
            } else {
                return "not_activated";
            }
        } else {
            return "failed";
        }
    }

    public function modules($table, $table2) {
        //$this->db = new dbconfig();
        $userid = $_SESSION['userid'];
        if ($_SESSION['admin'] == 0) {
            $this->query = "SELECT * FROM $table WHERE userid = '$userid'";
            $this->modules = $this->execute($this->query) or die("There is some problem in fetching modules and permissions");
        } else {
            $this->query = "SELECT * FROM $table2";
            $this->modules = $this->execute($this->query) or die("There is some problem in fetching modules and permissions");
        }
        while ($this->modules_data = $this->fetch_array($this->modules)) {
            $data[] = $this->modules_data;
        }
        //print_r($modules_data);
        return $data;
    }

    public function getregister($email, $password, $schoolname, $mobile, $table) {

        $this->email = htmlentities($email);
        $this->password = htmlentities($password);
        $this->schoolname = htmlentities($schoolname);
        $this->mobile = htmlentities($mobile);


        //$this->db = new dbconfig();
        $query1 = "SELECT email FROM $table WHERE email = '$this->email'";
        $check = $this->execute($query1) or die(mysql_error());
        $check_data = $this->num_rows($check);
        if ($check_data == 0) {
            $query2 = "SELECT * FROM $table";
            $check2 = $this->execute($query2) or die(mysql_error());
            $check_data2 = $this->num_rows($check2);
            if ($check_data2 > 0) {
                $alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                $length = 11;

                for ($i = 0; $i < $length; $i++) {
                    $ran = rand(0, strlen($alpha) - 1);
                    $this->key .= substr($alpha, $ran, 1);
                }
                //echo $key;
                $this->status = '0';
                $query2 = "INSERT INTO $table (email, password, schoolname, mobile, rand, status)  VALUES ('$this->email', '$this->password', '$this->schoolname', '$this->mobile', '$this->key', '$this->status')";
                $insert = $this->execute($query2) or die(mysql_error());

                //sending mail to user

                /*   $activation = "login.php?key=" . $key;
                  $your_email = 'you'; //CHANGE TO YOUR SETTINGS
                  $domain = $_SERVER["HTTP_HOST"]; //YOUR DOMAIN AND EXTENSION
                  $directory = dirname($_SERVER["PHP_SELF"]); //FOLDER WHERE THE FILES WILL BE LOCATED
                  require_once ('mail1.php');
                  $to = $email;

                  $message = "Welcome, " . $name . ". You must activate your account via this message to log in. Click the following link to do so: http://" . $domain . $directory . "/" . $activation;
                  $headers = "From: Your Site <" . $your_email . "@" . $domain . ">\r\n"; //MODIFY TO YOUR SETTINGS
                  $headers .= "Content-type: text/html\r\n";
                  smtpmailer($to, 'chilukurichandu.indian@gmail.com', 'Inventory', 'Activation Mail', $message);
                 */

                $to = $this->email;
                $activation = "index.php?key=" . $this->key;
                $subject = "Activate Account";
                $domain = $_SERVER["HTTP_HOST"]; //YOUR DOMAIN AND EXTENSION
                $directory = dirname($_SERVER["PHP_SELF"]);
                $message = "Welcome, " . $this->schoolname . ". You must activate your account via this message to log in. Click the following link to do so: http://" . $domain . $directory . "/" . $activation;

                $header = "From:abc@somedomain.com \r\n";
                $header = "Cc:afgh@somedomain.com \r\n";
                $header .= "MIME-Version: 1.0\r\n";
                $header .= "Content-type: text/html\r\n";
                $retval = mail($to, $subject, $message, $header);
                if ($retval == true) {
                    //echo "Message sent successfully...";
                } else {
                    //echo "Message could not be sent...";
                }



                //$this->error = new errors();
                if ($insert) {
                    //return $this->error->registration_sucess;
                    return "ok";
                } else {
                    //return $this->error->registration_failed;
                    return "fail";
                }
            } else {
                $alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                $length = 11;

                for ($i = 0; $i < $length; $i++) {
                    $ran = rand(0, strlen($alpha) - 1);
                    $this->key .= substr($alpha, $ran, 1);
                }
                //echo $key;
                $this->status = '0';
                $query2 = "INSERT INTO $table (email, password, schoolname, mobile, rand, status, adminstatus)  VALUES ('$this->email', '$this->password', '$this->schoolname', '$this->mobile', '$this->key', '$this->status', 1)";
                $insert = $this->execute($query2) or die(mysql_error());

                //sending mail to user

                /*   $activation = "login.php?key=" . $key;
                  $your_email = 'you'; //CHANGE TO YOUR SETTINGS
                  $domain = $_SERVER["HTTP_HOST"]; //YOUR DOMAIN AND EXTENSION
                  $directory = dirname($_SERVER["PHP_SELF"]); //FOLDER WHERE THE FILES WILL BE LOCATED
                  require_once ('mail1.php');
                  $to = $email;

                  $message = "Welcome, " . $name . ". You must activate your account via this message to log in. Click the following link to do so: http://" . $domain . $directory . "/" . $activation;
                  $headers = "From: Your Site <" . $your_email . "@" . $domain . ">\r\n"; //MODIFY TO YOUR SETTINGS
                  $headers .= "Content-type: text/html\r\n";
                  smtpmailer($to, 'chilukurichandu.indian@gmail.com', 'Inventory', 'Activation Mail', $message);
                 */

                $to = $this->email;
                $activation = "index.php?key=" . $this->key;
                $subject = "Activate Account";
                $domain = $_SERVER["HTTP_HOST"]; //YOUR DOMAIN AND EXTENSION
                $directory = dirname($_SERVER["PHP_SELF"]);
                $message = "Welcome, " . $this->schoolname . ". You must activate your account via this message to log in. Click the following link to do so: http://" . $domain . $directory . "/" . $activation;

                $header = "From:abc@somedomain.com \r\n";
                $header = "Cc:afgh@somedomain.com \r\n";
                $header .= "MIME-Version: 1.0\r\n";
                $header .= "Content-type: text/html\r\n";
                $retval = mail($to, $subject, $message, $header);
                if ($retval == true) {
                    //echo "Message sent successfully...";
                } else {
                    //echo "Message could not be sent...";
                }



                //$this->error = new errors();
                if ($insert) {
                    //return $this->error->registration_sucess;
                    return "ok";
                } else {
                    //return $this->error->registration_failed;
                    return "fail";
                }
            }
        } else {
            return "alreadyregistered";
        }
    }

    public function activation($key, $table) {

        // $this->db = new dbconfig();
        $query = "SELECT * FROM $table WHERE rand = '$key'";
        $check = $this->execute($query) or die("There is some problem in fetching the data for login");
        $check_data = $this->fetch_array($check);
        $no_rows = $this->num_rows($check) or die(mysql_error());
        if ($no_rows == 1) {
            if ($check_data['status'] === '0') {
                //updating the database active field with 'true'.
                $result = $this->execute(
                        "UPDATE $table SET status = '1'
             WHERE rand = '$key'") or print 'There was an error: ' . mysql_error();
            } else {
                return "activated";
            }
        }
    }

    public function forgot_password($table, $email) {
        $this->query = "select rand from $table where email = '$email'";
        $this->result = $this->execute($this->query);
        $this->result_data = $this->fetch_array($this->result);
        echo $this->result_data['rand'];
        $this->key = $this->result_data['rand'];

        $to = $email;
        $activation = "index.php?e_pass=" . $this->key;
        $subject = "Reset Password";
        $domain = $_SERVER["HTTP_HOST"]; //YOUR DOMAIN AND EXTENSION
        $directory = dirname($_SERVER["PHP_SELF"]);
        $message = "Welcome, " . $email . ". You must reset your password via this message to log in. Click the following link to do so: http://" . $domain . $directory . "/" . $activation;

        $header = "From:abc@somedomain.com \r\n";
        $header = "Cc:afgh@somedomain.com \r\n";
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-type: text/html\r\n";
        $retval = mail($to, $subject, $message, $header);
        if ($retval == true) {
            //echo "Message sent successfully...";
        } else {
            //echo "Message could not be sent...";
        }
        return TRUE;
    }

    // Admin Login process
    public function change_password($table, $c_password, $n_password) {
        $uid = $_SESSION['userid'];
        $this->current = $this->execute("SELECT password from $table WHERE userid = '$uid'") or die(mysql_error());
        $this->current_pass = $this->fetch_array($this->current);
        if ($this->current_pass['password'] == $c_password) {
            $this->change_pass = $this->execute("update users SET password = '$n_password' where userid = '$uid'") or die(mysql_error());
            if ($this->change_pass) {
                return TRUE;
            }
        } else {
            return FALSE;
        }
    }

    public function reset_password($table, $key, $password) {
        $this->query = "UPDATE $table SET password = '$password' WHERE rand = '$key'";

        $this->result = $this->execute($this->query);
        if ($this->result) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

}

?>